﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace JustWPFControls.Controls
{
    [TemplatePart(Name = "PART_RippleScale", Type = typeof(ScaleTransform))]
    [TemplatePart(Name = "PART_Ripple", Type = typeof(Ellipse))]
    public class JRippleMask : ContentControl
    {
        private Ellipse _ellipse = null!;
        private ScaleTransform _rippleScale = null!;

        static JRippleMask()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(JRippleMask), new FrameworkPropertyMetadata(typeof(JRippleMask)));
        }

        #region 依赖属性
        public Brush RippleColor
        {
            get { return (Brush)GetValue(RippleColorProperty); }
            set { SetValue(RippleColorProperty, value); }
        }

        public static readonly DependencyProperty RippleColorProperty =
            DependencyProperty.Register("RippleColor", typeof(Brush), typeof(JRippleMask), new PropertyMetadata(Brushes.Transparent));
        #endregion


        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _ellipse = (Ellipse)GetTemplateChild("PART_Ripple");
            _rippleScale = (ScaleTransform)GetTemplateChild("PART_RippleScale");
        }


        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            Point point = e.GetPosition(this);
            var radium = _ellipse.Width / 2;
            Canvas.SetTop(_ellipse, point.Y - radium);
            Canvas.SetLeft(_ellipse, point.X - radium);
            _ellipse.Visibility = Visibility.Visible;
            //TODO：Schuyler 动画效果待优化
            var scale = Math.Max(ActualWidth / _ellipse.Width, ActualHeight / _ellipse.Width) * 3;
            Storyboard sb = new Storyboard();
            DoubleAnimation scaleXAnimation = new DoubleAnimation(1, scale, TimeSpan.FromSeconds(0.4), FillBehavior.Stop);
            DoubleAnimation scaleYAnimation = new DoubleAnimation(1, scale, TimeSpan.FromSeconds(0.4), FillBehavior.Stop);
            DoubleAnimation opacityAnimation = new DoubleAnimation(0.4, 0, TimeSpan.FromSeconds(0.4), FillBehavior.Stop);
            sb.Children.Add(opacityAnimation);
            Storyboard.SetTarget(opacityAnimation, _ellipse);
            _rippleScale.BeginAnimation(ScaleTransform.ScaleXProperty, scaleXAnimation);
            _rippleScale.BeginAnimation(ScaleTransform.ScaleYProperty, scaleYAnimation);
            Storyboard.SetTargetProperty(opacityAnimation, new PropertyPath(Ellipse.OpacityProperty));
            sb.Completed += (s, e) =>
            {
                _ellipse.Visibility = Visibility.Collapsed;
            };
            sb.Begin();
        }
    }
}
